// fenxiao/shareCodes/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Distributor: {},
    userid: 0,
    mallName: "",
    setTitle: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      userid: options.userid,
      mallName: wx.getStorageSync('mallName')
    })
    that.getDistributorSet();
  },
  // 预览识别二维码
  preimg: function () {
    var url = this.data.Distributor.ACodeFileShowUrl
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  //下载二维码
  downLoadCode: function () {
    var that = this;
    wx.request({//请求地址
      url: app.globalData.host + '/api/Distributor/GetWxaCodeDownLoadUrl',
      data: {
        fileName: that.data.Distributor.ACodeFile
      },
      success: function (res) {
        console.log(res)
        if (res.data.Code == 0) {
          var fileUrl = 'https://' + res.data.Data
          //下载二维码及保存
          setTimeout(function () { that.saveFileImg(fileUrl) }, 2000)
        } else {
          wx.showModal({
            title: '提示',
            content: res.Message,
            showCancel: false
          })
        }
      }
    })
  },
  //保存二维码
  saveFileImg: function (fileUrl) {
    wx.downloadFile({
      url: fileUrl,
      success: function (res) {
        if (res.statusCode == 200) {
          var filePaths = res.tempFilePath
          wx.saveImageToPhotosAlbum({ //保存二维码
            filePath: filePaths,
            success(res) {
              wx.showToast({
                title: "保存成功",
                icon: 'success',
                duration: 2000
              })
            },
            fail(res) {
              console.log(res)
            }
          })
        }
      }
    })
  },
  //获取分销商信息，如果分销商不存在，则创建新的
  GetDistributorInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetDistributorAndCreate',
      data: {
        userId: that.data.userid,
        storeId: app.globalData.storeId
      },
      success: function (res) {
        if (res.data.Code == 0) {
          var dis = res.data.Data
          that.setData({
            firstBrokerageRise: dis.DisGrade.FirstBrokerageRise
          })
          that.GetAgentCode(dis)
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  //获取分销商二维码
  GetAgentCode: function (dis) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetWxaCodeFile',
      data: {
        id: dis.Id,
        weappid: 0,
        storeappid: app.globalData.storeAppId
      },
      success: function (res) {
        if (res.data.Code == 0) {
          var disCode = res.data
          dis.ACodeFile = disCode.Data;
          dis.ACodeFileShowUrl = app.globalData.fenxiao_code + disCode.Data;
          that.setData({ Distributor: dis });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  
  getDistributorSet: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetDistributorSet?storeId=' + app.globalData.storeId,
      success: function (res) {
       if(res.data.Code == 0){
         that.setData({
           setTitle: res.data.Data
         })
       }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.GetDistributorInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  //分享
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.mallName + '购物即有返佣，快来哦',
      imageUrl: that.data.Distributor.ACodeFileShowUrl,
      path: '/fenxiao/shareCodes/index?userid=' + that.data.Distributor.UserId,
      success: function (res) {
        // 转发
        wx.showModal({
          title: '提示',
          content: '转发成功',
          showCancel: false
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})